﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BusinessLogic.ClassFactory;
using BusinessLogic.BusinessRules;
using DataAccess;
using Model;
using log4net;
using ValidateCommon;

namespace RichECMS.ServerPages.SysManage
{
    public partial class WebSite : Util.BasePage.BasePage
    {
        private static readonly ILog objLog = LogManager.GetLogger(typeof(WebSite));
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowPageInfo();
            }
        }

        private void ShowPageInfo()
        {
            try
            {
                using (WebSiteFactory objFactory = new WebSiteFactory())
                {
                    BusinessLogic.BusinessRules.WebSite objWebSite = objFactory.CreatorBySql(string.Format(" intWebSiteID={0}", State.intWSID));
                    if (objWebSite != null)
                    {
                        txtTitle.InnerText = objWebSite.DataItem.chvTitle;
                        txtKeyWords.InnerText = objWebSite.DataItem.chvKeyWords;
                        txtDescription.InnerText = objWebSite.DataItem.chvDescription;
                        this.txtIndexTemplateID.Value = objWebSite.DataItem.intTmplID.ToString(); ;
                    }
                }
                using (SystemVariableFactory objFactory = new SystemVariableFactory())
                {
                    BusinessLogic.BusinessRules.SystemVariable objSystemVariable = objFactory.Creator();
                    if (objSystemVariable != null)
                    {
                        txtIssuePath.InnerText = objSystemVariable.DataItem.chvIssuePath;
                        txtWebUrl.InnerText = objSystemVariable.DataItem.chvWebUrl;
                    }
                }
            }
            catch (Exception ex)
            {
                Alert(ex.ToString().Replace("\r\n", " "));
            }
        }

        /// <summary>
        /// 验证输入
        /// </summary>
        private bool ValidateValues()
        {
            bool bolIsValidate = true;
            if (this.txtTitle.Value.Trim().Length == 0)
            {
                Alert("请填写标题");
                bolIsValidate = false;
            }
            if (this.txtIndexTemplateID.Value.Trim().Length == 0)
            {
                Alert("请填写网站首页模版ID,多个模版之间使用,分隔");
                bolIsValidate = false;
            }
            else
            {
                string[] strIDS = this.txtIndexTemplateID.Value.Trim().Split(',');
                foreach (string strItem in strIDS)
                {
                    if (!ParamAuth.IsNumber(strItem))
                    {
                        Alert("请填写网站首页模版ID,多个模版之间使用,分隔");
                        bolIsValidate = false;
                        break;
                    }
                }
            }
            return bolIsValidate;
        }

        /// <summary>
        /// 提交网站SEO信息和系统配置信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void SubmitBtnHander(object sender, ImageClickEventArgs e)
        {
            if (!ValidateValues())
            {
                return;
            }

            using (DBManager _DBManager = new DBManager())
            {
                try
                {
                    _DBManager.BeginTransaction();
                    WebSiteFactory objFactory = new WebSiteFactory(_DBManager);
                    BusinessLogic.BusinessRules.WebSite objWebSite = objFactory.Creator(State.intWSID);
                    if (objWebSite != null)
                    {
                        objWebSite.DataItem.chvDescription = txtDescription.InnerText;
                        objWebSite.DataItem.chvKeyWords = txtKeyWords.InnerText;
                        objWebSite.DataItem.chvTitle = txtTitle.InnerText;
                        objWebSite.DataItem.chvName = txtTitle.InnerText;
                        //objWebSite.DataItem.chvIssuePath = txtIssuePath.InnerText;
                        objWebSite.DataItem.chvWebUrl = txtWebUrl.InnerText;
                        objWebSite.DataItem.intTmplID = this.txtIndexTemplateID.Value;
                        objWebSite.Update();
                    }

                    SystemVariableFactory objSystemVariableFactory = new SystemVariableFactory(_DBManager);
                    SystemVariable objSystemVariable = objSystemVariableFactory.CreatorBySql(string.Format(" intWSID={0}", State.intWSID));
                    if (objSystemVariable != null)
                    {
                        //objSystemVariable.DataItem.chvIssuePath = txtIssuePath.InnerText;
                        //objSystemVariable.DataItem.chvPicPath = txtIssuePath.InnerText;
                        //objSystemVariable.DataItem.chvMiniPicPath = txtIssuePath.InnerText;
                        //objSystemVariable.DataItem.chvTemplatePath = txtIssuePath.InnerText;
                        objSystemVariable.DataItem.chvWebUrl = txtWebUrl.InnerText;

                        //string strRepleaceOld = "serverpages\\sysmanage\\webSite.aspx".ToLower();
                        //string strwebpath = Server.MapPath(Request.Path).ToLower().Replace(strRepleaceOld, "");

                        //SystemVariable.IssuePath = strwebpath + "news\\" + objSystemVariable.DataItem.chvIssuePath;
                        //SystemVariable.MiniPicPath = strwebpath + "Pcfiles\\" + objSystemVariable.DataItem.chvMiniPicPath;
                        //SystemVariable.PicPath = strwebpath + "Pcfiles\\" + objSystemVariable.DataItem.chvPicPath;
                        State.LoginUser.WebUrl = objSystemVariable.DataItem.chvWebUrl;
                        //SystemVariable.TemplatePath = strwebpath + "TemplateFile\\" + objSystemVariable.DataItem.chvTemplatePath;
                    }
                    objSystemVariable.Update();

                    _DBManager.Commit();
                    objSystemVariableFactory.Dispose();
                    objFactory.Dispose();
                }
                catch (Exception ex)
                {
                    _DBManager.Rollback();
                    objLog.Error(ex.Message + ex.Source);
                    Alert(ex.Message);
                }
                finally
                {
                    _DBManager.Dispose();
                }
                Alert("修改成功");
            }
        }
    }
}